package sdu.attendancechecker.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sdu.attendancechecker.dto.Admin;
import sdu.attendancechecker.services.AdminService;
import sdu.attendancechecker.services.PasswordService;

public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("forgot") != null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("admin/forgot.jsp");
			dispatcher.forward(request, response);
			return;
		}
		if (request.getParameter("database") != null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("admin/admin_database.jsp");
			dispatcher.forward(request, response);
			return;
		}
		if (request.getSession().getAttribute("admin") == null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("admin/login");
			dispatcher.forward(request, response);
			return;
		}
		if (request.getParameter("logout") != null) {
			request.getSession().removeAttribute("admin");
			this.doGet(request, response);
			return;
		}

		String url = request.getPathInfo();
		System.out.println("URL: " + url);
		List teachers = AdminService.getTeachers();
		request.setAttribute("teachers", teachers);
		if (teachers != null)
			System.out.println("teachers: " + teachers.toString());
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("admin/teachers.jsp");
		dispatcher.forward(request, response);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("email_forgot") != null) {
			System.out.println("Kirid");
			try {
				String pwd = PasswordService.generatePassword();
				PasswordService.sendPasswordTo(
						request.getParameter("email_forgot"), pwd);
				AdminService.updatePasswordByEmail(
						request.getParameter("email_forgot"), pwd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			this.doGet(request, response);
			return;
		}
		if (request.getParameter("email") != null
				&& request.getParameter("password") != null) {
			Admin admin = AdminService.login(request.getParameter("email"),
					request.getParameter("password"));
			request.getSession().setAttribute("admin", admin);
			this.doGet(request, response);
			return;
		}
		if (request.getSession().getAttribute("admin") == null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("admin/login");
			dispatcher.forward(request, response);
			return;
		}

		String command = request.getParameter("command");
		response.setContentType("text/html;charset=UTF-8");

		if (command.equals("delete")) {
			Integer id = Integer.parseInt(request.getParameter("id"));
			boolean success = AdminService.deleteTeacherById(id);
			response.getWriter().println(success ? 1 : 0);
			return;
		}
		if (command.equals("add")) {
			System.out.println("Adding");
			Integer id;
			try {
				id = Integer.parseInt(request.getParameter("id"));
			} catch (Exception e) {
				response.getWriter().println(2);
				return;
			}
			String name = request.getParameter("name");
			String surname = request.getParameter("surname");
			String email = request.getParameter("email");
			boolean success = AdminService.addTeacher(id, name, surname, email);
			System.out.println(success);
			response.getWriter().println(success ? 1 : 0);
		}
	}

}
